﻿using DTM.Model;
using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace DTM.WinApp.Players
{
    public partial class PlayersForm : Form, IPlayersView
    {
        #region Fields

        private readonly IPlayersPresenter _presenter;

        #endregion Fields

        #region Events

        public event EventHandler<EventArgs> AddPlayer;

        public event EventHandler<EventArgs<IPlayer>> EditPlayer;

        #endregion Events

        #region Properties

        public List<IPlayer> DataSource
        {
            get { return (List<IPlayer>)dataGridView.DataSource; }
            set { dataGridView.DataSource = value; }
        }

        #endregion Properties

        #region Constructors

        public PlayersForm(IPlayersPresenter presenter)
        {
            DTM.Core.Utils.Validate.NotNull<ArgumentNullException>(presenter, "presenter");
            _presenter = presenter;

            InitializeComponent();
            dataGridView.AutoGenerateColumns = false;

            _presenter.Initialize(this);
        }

        #endregion Constructors

        #region Methods

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (AddPlayer != null)
            {
                AddPlayer(this, new EventArgs());
            }
        }

        private void dataGridView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex == -1)
            {
                return;
            }
            var player = DataSource[e.RowIndex];
            if (EditPlayer != null)
            {
                EditPlayer(this, new EventArgs<IPlayer>(player));
            }
        }

        #endregion Methods
    }
}